package com.marvels.approve.process.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.approve.loan.dto.LoanAfterInfoDTO;
import com.marvels.approve.manager.dto.OpinionDTO;
import com.marvels.approve.process.dto.FaceSigAppointmentDTO;
import com.marvels.approve.process.dto.FacesigOperationDTO;
import com.marvels.approve.process.dto.LaAttachAtmeTypeViewDTO;
import com.marvels.approve.process.dto.LaAttachmentDTO;
import com.marvels.approve.process.service.AttachmentInfoService;
import com.marvels.approve.process.service.IFaceCheckAssignmentService;
import com.marvels.approve.process.service.IFaceSigService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

@RestController
@RequestMapping("/approve/video/service")
public class VideoCheckController {

	@Autowired
	private IFaceCheckAssignmentService faceCheckAssignmentService;
	
	@Autowired
	private AttachmentInfoService attachmentInfoService;
	
	@Autowired
	private IFaceSigService faceSigService;
	
	
	/***
	 * 更新面签状态
	 * @param dto
	 * @return
	 */
	@RequestMapping("/modifyInterviewSignStatus")
	@ResponseBody
	public MarvelsResultDTO startFacesig(@RequestBody(required=false) FaceSigAppointmentDTO dto){
		int count=0;
		try {
			count =  faceCheckAssignmentService.modifyInterviewSignStatus(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), count);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	/**
	 * 更新合同金额
	 * @param dto
	 * @return
	 */
	@RequestMapping("/modifyCompactAmount")
	@ResponseBody
	public MarvelsResultDTO modifyCompactAmount(@RequestBody(required=false) FacesigOperationDTO dto){
		Integer count= 0;
		try {
			count =  faceCheckAssignmentService.modifyCompactAmount(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), count);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	
	}
	
	/***
	 * 映像文件
	 * @param dto
	 * @return
	 */
	@RequestMapping("/getAttachmentInfo")
	@ResponseBody
	public MarvelsResultDTO getAttachmentInfo(@RequestBody(required=false) LaAttachmentDTO laAttachmentDTO){
		List<LaAttachAtmeTypeViewDTO> data= null;
		try {
			data =  attachmentInfoService.selectAttachmentInfo(laAttachmentDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), data);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	
	}
	
	/**
	 * 获取金融服务合同
	 * @param laAttachmentDTO
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/showContract/{loanId}/{templateNo}")
	@ResponseBody
	public MarvelsResultDTO getServicesContracts(@PathVariable("loanId") String loanId, @PathVariable("templateNo") String templateNo){
		Map map= null;
		try {
			map =  faceCheckAssignmentService.previewLoanCompact(loanId,templateNo);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), map);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	
	}
	
	/***
	 * 获取合同金额
	 * 
	 * @return
	 */
	@RequestMapping("/getAppAmount/{loanId}/{appointmentId}")
	@ResponseBody
	public MarvelsResultDTO getAppAmount(@PathVariable("loanId") String loanId,@PathVariable("appointmentId")String appointmentId){
		Map<String, Object> list= null;
		try {
			list =  faceCheckAssignmentService.searchAppAmount(loanId,appointmentId);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
		
	}
	/**
	 * 开始面签
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/pushFaceSig")
	@ResponseBody
	public MarvelsResultDTO pushFaceSig(@RequestBody(required=false) FaceSigAppointmentDTO dto){
		String list = null;
		try {
			list =  faceSigService.pushFaceSig(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**
	 * 查询审批结果
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/queryLoanApplyCaseLogs/{loanId}")
	@ResponseBody
	public MarvelsResultDTO queryLoanApplyCaseLogs(@PathVariable String loanId){
		List<OpinionDTO> list = null;
		try {
			list =  faceCheckAssignmentService.queryLoanApplyCaseLogs(loanId);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	/***
	 * 新增面谈/面签结果
	 * @param dto
	 * @param appointmentId
	 * @param type
	 * @return
	 */
	@RequestMapping("/addInterviewResult")
	@ResponseBody
	public MarvelsResultDTO addInterviewResult(@RequestBody(required=false) FacesigOperationDTO facesigOperationDto){
		Integer list = null;
		try {
			list =  faceCheckAssignmentService.addInterviewResult(facesigOperationDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}
	
	/***
	 * 发送电子合同
	 * @param loanId
	 * @param appointmentId
	 * @param isResend
	 * @param returnCause
	 * @return
	 */
	@RequestMapping("/SendContract")
	@ResponseBody
	public MarvelsResultDTO SendContract(@RequestBody Map<String,String> paramMap){
		try {
			Integer dto =  faceCheckAssignmentService.sendLoanCompact(paramMap.get("loanId"), paramMap.get("isResend"), paramMap.get("appointmentId"), paramMap.get("returnCause"));
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	
	}
	
	/***
	 * 重签电子合同
	 * @param loanId
	 * @param appointmentId
	 * @param isResend
	 * @param returnCause
	 * @return
	 */
	@RequestMapping("/resetCompactSign")
	@ResponseBody
	public MarvelsResultDTO resetCompactSign(@RequestBody FacesigOperationDTO facesigOperationDTO){
		try {
			Integer dto =  faceCheckAssignmentService.resetCompactSign(facesigOperationDTO,true);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	
	}
	
	/**
	 * 预览还款计划
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/previewRepaymentPlan/{loanId}")
	@ResponseBody
	public MarvelsResultDTO previewRepaymentPlan(@PathVariable("loanId") String loanId){
		try {
			LoanAfterInfoDTO dto =  faceCheckAssignmentService.searchRepaymentPlan(loanId);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("marvels_error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e);
		}
	
	}
	
	
	
}
	
	
